﻿using System;
using System.Collections.Generic;
using System.Drawing;
using SVM;
using Emgu.CV;
using Emgu.CV.Structure;
namespace FFTConv
{
    class EmguSample
    {
        public Image<Gray, byte> Image;
        Bitmap bitmap;
        public Matrix<double> Gabor;
        public double Category;
        public EmguSample(double category, Bitmap bitmap)
        {
            this.Category = category;
            this.PrepareImage(bitmap);
        }
        public EmguSample(Bitmap bitmap)
        {
            
            this.PrepareImage(bitmap);
        }
        public void PrepareImage(Bitmap  bitmap)
        {
            
            //this.Bitmap = new Bitmap(filename);
            this.Image = new Image<Gray, byte>(bitmap.Width, bitmap.Height);
            for (int y = 0; y < bitmap.Height; y++)
                for (int x = 0; x < bitmap.Width; x++)
                {
                    
                        this.Image[y,x] = new Gray(ColorToGray(bitmap.GetPixel(x, y)));
                  
                }
        }
        public static double ColorToGray(Color color)
        {
            //Gray = 0.299 * R + 0.587 * G + 0.114 * B
          
            double gray = ((((int)color.R * 77) + ((int)color.G * 151)) + ((int)color.B * 28))/256;

            if (gray < 0) gray = 0.0;
            if (gray > 255) gray = 255.0;

            return gray;
        }
        public Node[] ToNodes()
        {
            List<Node> nodes = new List<SVM.Node>();
            for (int y = 0; y < this.Gabor.Height; y++)
            {
                for (int x = 0; x < this.Gabor.Width; x++)
                    try
                    {
                        int i = y * this.Gabor.Width + x;
                        nodes.Add(new SVM.Node(i + 1, this.Gabor[y, x]));
                    }
                    catch (Exception exp)
                    {
                        throw exp;
                    }
            }
            return nodes.ToArray();
        }

    }
}
